<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Sport extends Model implements Jqgrid_GridListener{

	public $sport=NULL;

	public function count($criteria = false){
		return ORM::factory("dao_sport")->count_all();
	}
		
	public function getList($criteria = false){
			
		if (!$criteria)	$sport_list = ORM::factory("dao_sport")->find_all()->as_array();
		else{
			if ($criteria->isSearch){
				$sport_list = ORM::factory("dao_sport")
								->where($criteria->getSQLField("sports"),$criteria->getSQLOper(), $criteria->getSQLSearchString())
								->order_by($criteria->sidx,$criteria->sord)
								->limit($criteria->limit)
								->offset($criteria->calcOffset())
								->find_all()
								->as_array();
						
			}else{
				$sport_list = ORM::factory("dao_sport")->order_by($criteria->sidx,$criteria->sord)->limit($criteria->limit)->offset($criteria->calcOffset())->find_all()->as_array(); 
			}
		} 

		$dataset = array();
		foreach ($sport_list as $sport){
			$dataset[$sport->id] = array($sport->sport_name,$sport->description);
		}
			
		return $dataset;
	}

	/**
	 *
	 * get all sport from an id
	 * @param int $minId
	 * @param int $maxAmount
	 * @return array of sports
	 */
	public function getListFromMinID($minId, $maxAmount){
		return ORM::factory("dao_sport")->where("id",">",$minId)->order_by("id", "ASC")->limit($maxAmount)->find_all()->as_array();
	}

	public function get($id){
		return ORM::factory("dao_sport")->find($id);
	}

	public function getAll(){
		return ORM::factory("dao_sport")->find_all()->as_array();
	}

	public function getName(){
		return $this->sport->sport_name;
	}

	public function getDescription(){
		return $this->sport->description;
	}


	public function save($id, array $data){
		$this->sport = ($id > 0) ? ORM::factory("dao_sport",$id) : ORM::factory("dao_sport");
		$this->sport->sport_name = $data['sport_name'];
		$this->sport->description = $data['description'];
		$this->sport->save();
		return $this->sport->id;
	}

	public function delete($id){
		$sport = ORM::factory("dao_sport",$id);
		$sport->delete();
		return $id;
	}

	
	/**
	*
	* return an entity sport by name
	* @param string $name
	* @return sport
	*/
	public function getByName($name){
		return ORM::factory("dao_sport")->where("sport_name","=",$name)->find();
	}

}

?>

